//
//  MovieDetailViewController.m
//  12.30_homework_豆瓣电影MVC
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "UIImageView+WebCache.h"
#import "MovieModel.h"
#import "LXC_DataBaseHandle.h"

@interface MovieDetailViewController ()

@property (retain, nonatomic) IBOutlet UIImageView *imgView;

@property (retain, nonatomic) IBOutlet UILabel *Mytitle;

@property (retain, nonatomic) IBOutlet UILabel *stars;

@property (retain, nonatomic) IBOutlet UILabel *pubdate;

@property (nonatomic, retain) NSMutableArray *arrData;

@property (nonatomic, retain) MovieModel *model;

@property (nonatomic, assign) BOOL flag;

@end

@implementation MovieDetailViewController

- (void)dealloc {
    [_imgView release];
    [_Mytitle release];
    [_stars release];
    [_pubdate release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"电影详情";
    self.navigationController.navigationBar.translucent = NO;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:path];
    self.arrData = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        MovieModel *model = [MovieModel movie];
        [model setValuesForKeysWithDictionary:dic];
        [_arrData addObject:model];
    }
    
    self.model = _arrData[_indexPath.row];
    
    self.flag = [[LXC_DataBaseHandle shareDataBaseHandle] selectModelFromMovie:_model];
    
    NSString *rightButtonTitle = @"";
    
    if (!_flag) {
        rightButtonTitle = @"收藏";
    }else {
        rightButtonTitle = @"cancel";
    }
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:rightButtonTitle style:(UIBarButtonItemStylePlain) target:self action:@selector(collectionAction:)];
    
    
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:_model.image]];
    
    self.Mytitle.text = [NSString stringWithFormat:@"名称 : %@", _model.title];
    self.stars.text = [NSString stringWithFormat:@"评分 : %@", _model.rating];
    self.pubdate.text = [NSString stringWithFormat:@"上映时间 : %@", _model.pubdate];
    
    
}
- (void)collectionAction:(UIBarButtonItem *)button {
    
    NSInteger goinState = [[LXC_DataBaseHandle shareDataBaseHandle] selectLoginStateFromUserTab:1];
    
    if (!goinState) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请登陆后查看" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];

        
    }else {
        
        self.flag = [[LXC_DataBaseHandle shareDataBaseHandle] selectModelFromMovie:_model];
        
        if (!_flag) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否收藏" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                [[LXC_DataBaseHandle shareDataBaseHandle] insertModelToMovie:_model];
                
                self.navigationItem.rightBarButtonItem.title = @"cancle";
                
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:nil];
            [alert addAction:action1];
            [alert addAction:action2];
            [self presentViewController:alert animated:YES completion:nil];
        }else {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否取消收藏" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                
                [[LXC_DataBaseHandle shareDataBaseHandle] deletemodelFromMovie:_model];
                
                self.navigationItem.rightBarButtonItem.title = @"收藏";
                
                
            }];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
            [alert addAction:action];
            [alert addAction:action1];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
    }
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
